//
//  WDChapter2.c
//  ProjectM
//
//  Created by gaofeng on 2021/4/17.
//

#include "WDChapter2.h"
void title201(void);
void title201a(void);
void title201b(void);
void title201c(void);
void title210(void);
void Reverse(int R[], int f, int t);
void t210(int a[], int n, int p);
void title202(void);
void title202a(void);

void testfun1(void);
int deletMini(SqList *sq, int* v);
void runChapter(void){
//    title201a();
//    title201b();
    //title202a();
    //testfun1();
     title210();
}
void title210(void) {
    int a[10] = { 0,1,2,3,4,5,6,7,8,9 };
    //Reverse(a, 0, 4);
    //Reverse(a, 5, 9);
    //Reverse(a, 0, 9);
    t210(a, 10, 4);
    for (int i = 0; i < 10; i++)
    {
        printf("%d ", a[i]);
    }

}
void t210(int a[], int n, int p) {
    int temp = 0;
    for (int i = 0; i < n/2; i++)
    {
        temp = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = temp;
    }
    for (int  i = 0; i < (n-p)/2; i++)
    {
        temp = a[i];
        a[i] = a[n - p - i - 1];
        a[n - i - p - 1] = temp;
    }
    for (int  i = n-p; i < p/2; i++)
    {
        temp = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = temp;
    }
}
void Reverse(int R[], int f, int t) {
    int i, temp;
    for ( i = 0; i < (t-f+1)/2; i++)
    {
        temp = R[f + i];
        R[f + i] = R[t - i];
        R[t - i] = temp;
    }
}
void title202a(void){
    int a[8] = {8,5,4,3,6,5,4,9};
    YKList lh = creatListHeadNode(a, 8);
    YKList ll =  deletElemHeadListA(lh, 5);
    printList(ll);
    printList(lh);
}

void testfun1(){
    int a[8] = {8,5,4,3,6,5,4,9};
    YKList lt = creatListb(a, 8);
    printList(lt);
    YKList ll = overturnListB(lt);
    printList(ll);
 
}

void title201(){
    SqList *sq = initSqlist();
    printSqList(sq);
    int flag,value;
    flag = deletMini(sq, &value);
    if (flag ==0)
    {
        printf("v=%d,l=%d\n", value,sq->length);
        printSqList(sq);
    }
    else
    {
        printf("%d\n",flag);
    }

}
// 删除顺序表中最小的一个元素，用最末尾的一个填充
int deletMini(SqList *sq, int* v) {
    if (sq == NULL) {
        return 1;
    }
    if (sq->length ==0){
        return 2;
    }
    int id = 0;
    int min = sq->datas[0];
    for (int i = 1; i < sq->length; i++)
    {
        if (min>sq->datas[i]){
            min = sq->datas[i];
            id = i;
        }
    }
    *v = min;
    sq->datas[id] = sq->datas[--sq->length];
    return 0;
}
void title201a(){
    int a[8] = {8,5,4,3,6,5,4,9};
    YKList lt = creatListb(a, 8);
    deletElemtea(&lt,5);
    printList(lt);
}
// 存在断链
void title201c(){
    int a[8] = {8,5,4,3,6,5,4,9};
    YKList lt = creatListb(a, 8);
    YKNode ls = *lt;
    deletElemtec(&ls,5);
    printNodes(ls);
}
void title202(void){
    SqList* sq = initSqlist();
    overturnSqList(sq);
    printSqList(sq);
}
